package com.csxy.six.controller;

import com.csxy.six.R2;
import com.csxy.six.entity.Blacklist;
import com.csxy.six.service.BlacklistService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/blacklist")
@Tag(name = "黑名单管理", description = "黑名单管理相关接口")
@SecurityRequirement(name = "JWT")
public class BlacklistController {

    @Autowired
    private BlacklistService blacklistService;

    @GetMapping
    @Operation(summary = "获取黑名单列表", description = "获取所有黑名单记录")
    public R2<List<Blacklist>> getAllBlacklist() {
        return blacklistService.getAllBlacklist();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取黑名单详情", description = "根据ID获取黑名单详情")
    public R2<Blacklist> getBlacklistById(@PathVariable @Parameter(description = "黑名单记录ID") Long id) {
        return blacklistService.getBlacklistById(id);
    }

    @PostMapping
    @Operation(summary = "添加用户到黑名单", description = "将用户添加到黑名单")
    public R2<Void> addToBlacklist(
            @RequestParam @Parameter(description = "用户ID") Long userId,
            @RequestParam @Parameter(description = "原因") String reason,
            @RequestParam(required = false) @Parameter(description = "结束时间") LocalDateTime endTime) {
        return blacklistService.addToBlacklist(userId, reason, endTime);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新黑名单记录", description = "更新黑名单信息")
    public R2<Blacklist> updateBlacklist(
            @PathVariable @Parameter(description = "黑名单记录ID") Long id,
            @RequestParam(required = false) @Parameter(description = "原因") String reason,
            @RequestParam(required = false) @Parameter(description = "结束时间") LocalDateTime endTime) {
        return blacklistService.updateBlacklist(id, reason, endTime);
    }

    @DeleteMapping("/user/{userId}")
    @Operation(summary = "从黑名单移除用户", description = "将指定用户从黑名单中移除")
    public R2<Void> removeFromBlacklistByUserId(
            @PathVariable @Parameter(description = "用户ID") Long userId) {
        return blacklistService.removeFromBlacklist(userId);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除黑名单记录", description = "删除指定黑名单记录")
    public R2<Void> removeFromBlacklistById(@PathVariable @Parameter(description = "黑名单记录ID") Long id) {
        return blacklistService.removeFromBlacklistById(id);
    }

    @GetMapping("/check")
    @Operation(summary = "检查用户是否在黑名单", description = "检查指定用户是否在黑名单中")
    public R2<Boolean> checkIfBanned(@RequestParam @Parameter(description = "用户ID") Long userId) {
        return blacklistService.checkIfBanned(userId);
    }
}